package com.central.apps.controller;

import com.central.app.service.HandlerService;
import io.swagger.annotations.Api;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "采购管理采购申请执行明细接口")
@RequestMapping("/purappexls")
public class PurappexlsController {
    @Autowired
    private HandlerService handlerService;
    @PostMapping("/purappexls")
    public Map<String,Object> purappexls (@RequestBody Map<String,Object> params) throws Exception{
        Integer page = MapUtils.getInteger(params, "page");
        Integer limit = MapUtils.getInteger(params, "limit");
        String subjectno = MapUtils.getString(params, "subjectno");
        String itemtype = MapUtils.getString(params, "itemtype");
        String itemdesc = MapUtils.getString(params, "itemdesc");
        String prnum = MapUtils.getString(params, "prnum");
        String plasigtus = MapUtils.getString(params, "plasigtus");
        String orderextus = MapUtils.getString(params, "orderextus");
        String commerceorder = MapUtils.getString(params, "commerceorder");
        String ponum = MapUtils.getString(params, "ponum");
        String applytime_where_date_from = MapUtils.getString(params, "applytime_where_date_from");
        String applytime_where_date_to = MapUtils.getString(params, "applytime_where_date_to");
        ArrayList<String> strings = new ArrayList<>();
        strings.add(subjectno);//0
        strings.add("subjectno");//1
        strings.add(itemtype);//2
        strings.add("itemtype");//3
        strings.add(itemdesc);//4
        strings.add("itemdesc");//5
        strings.add(prnum);//6
        strings.add("prnum");//7
        strings.add(plasigtus);//8
        strings.add("plasigtus");//9
        strings.add(orderextus);//10
        strings.add("orderextus");//11
        strings.add(commerceorder);//12
        strings.add("commerceorder");//13
        strings.add(ponum);//14
        strings.add("ponum");//15
        HashMap<String, Object> hashMap1 = new HashMap<>();
        //判断列表页查询条件是否为空
        List<Map<String, Object>> bySql=null;
//        page =(page -1)*limit;
//        String pageLimit =" limit "+page+","+limit;
        if (subjectno==null&&itemtype==null&&itemdesc==null&&prnum==null&&plasigtus==null&&orderextus==null&&commerceorder==null&&ponum==null){
            bySql = findAllSql(null);
        }else {
            String list = " t.";
            String list1=" like";
            String list3="";
            //String list2="and";
            for (int i=0;i<strings.size()-4;i=i+2){
                String s = strings.get(i);
                if (s!=null){
                    if(list3==""){
                            list3=list3+list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }else{
                        list3=list3+" and "+list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
            }
            String list4 = " i.";
            String list5=" like";
            String list6="";
            for (int i=12;i<strings.size();i=i+2){
                String s = strings.get(i);
                if (s!=null){
                    if(list6==""){
                        if (list3==""){
                            list6=list6+list4+strings.get(i+1)+list5+"'%"+s+"%'";
                        }else {
                            list6="and" + list6+list4+strings.get(i+1)+list5+"'%"+s+"%'";
                        }

                    }else{
                        list6=list6+" and "+list4+strings.get(i+1)+list5+"'%"+s+"%'";
                    }
                }
            }
            String list7 = " a.";
            String list8=" like";
            String list9="";
            for (int i=0;i<strings.size()-4;i=i+2){
                String s = strings.get(i);
                if (s!=null){
                    if(list3==""){
                        list3=list3+list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }else{
                        list3=list3+" and "+list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
            }
//            String str="";
//            if (StringUtils.isNotEmpty("applytime_where_date_from")&&StringUtils.isNotEmpty("applytime_where_date_to")){
//                str =" and a.applytime>applytime_where_date_from and a.applytime< applytime_where_date_t";
//            }
            bySql = handlerService.findBySql("SELECT\tDISTINCT \n" +
                    "                                        t.id,\n" +
                    "                                        t.subjectno,\n" +
                    "                                        a.createuserdesc,\n" +
                    "                                        t.itemtype,\n" +
                    "                                        t.itemnum,\n" +
                    "                                        t.itemdesc,\n" +
                    "                                        t.specifications,\n" +
                    "                                        t.model,\n" +
                    "                                        t.reasonpur,\n" +
                    "                                        t.maintechtors,\n" +
                    "                                        t.packform,\n" +
                    "                                        t.qualitygrade,\n" +
                    "                                        t.manufacturer,\n" +
                    "                                        t.advisupplier,\n" +
                    "                                        t.orderunit,\n" +
                    "                                        t.orderqty,\n" +
                    "                                        t.unitcost,\n" +
                    "                                        t.linecost,\n" +
                    "                                        t.purchasemethod,\n" +
                    "                                        a.deptcode,\n" +
                    "                                        (select fullname from sys_dept where code=a.deptcode) as fullname,\n" +
                    "                                        t.prnum,\n" +
                    "                                        b.prcnum,\n" +
                    "                                        a.applytime,\n" +
                    "                                        t.acceptmentsSelectBox,\n" +
                    "                                        t.reqdeliverydate,\n" +
                    "                                        d.createtime as createtime1,\n" +
                    "                                        e.createtime as createtime2,\n" +
                    "                                        g.procurexetor,\n" +
                    "                                        i.deliverycycle,\n" +
                    "                                        i.commerceorder,\n" +
                    "                                        i.ponum,\n" +
                    "                                        i.unitcost as unitcost1,\n" +
                    "                                        i.orderqty as orderqty1,\n" +
                    "                                        i.amexclutax\n" +
                    "                                        FROM\n" +
                    "                                        gq_prline t\n" +
                    "                                        LEFT JOIN gq_pr a ON t.prnum = a.prnum and a.status='已批准'\n" +
                    "                                        LEFT JOIN gq_prchange b on a.prnum =b.prnum and b.status='已批准'\n" +
                    "                                        left join gq_prtdetailline c on t.id= c.prlid and c.receiveqty>0\n" +
                    "                                        left join gq_matusetrans d on c.batchnum = d.lotnum and d.type='接收'\n" +
                    "                                        left join gq_matusetrans e on e.lotnum=c.batchnum and e.type in('领用出库','报废出库','销售出库','返厂维修')\n" +
                    "                                        left join gq_ppline f on f.prlid=t.id\n" +
                    "                                        left join gq_purchaseplan g on g.id=f.ppid and g.status='已批准' \n" +
                    "                                        left join gq_poline h on h.prlid =t.id\n" +
                    "                                        left join gq_posline i on i.id = h.poslid \n" +
                    "                                        where "+list3+"  "+list6+" and t.subjectno IS not NULL and t.prnum in (select prnum from gq_pr where prnum=t.prnum)  and g.ppnum IS NOT NULL ");
        }

        List<Map<String,Object>> collect = bySql.stream().skip((page-1)*limit).limit(limit).collect(Collectors.toList());

        String code="0";
        hashMap1.put("count",bySql.size());
        hashMap1.put("code",code);
        hashMap1.put("data",collect);

        return hashMap1;
    }

    public List<Map<String, Object>> findAllSql(String ids) {
        List<Map<String, Object>> bySql;
        String sql = "SELECT\tDISTINCT \n" +
                "                                        t.id,\n" +
                "                                        t.subjectno,\n" +
                "                                        a.createuserdesc,\n" +
                "                                        t.itemtype,\n" +
                "                                        t.itemnum,\n" +
                "                                        t.itemdesc,\n" +
                "                                        t.specifications,\n" +
                "                                        t.model,\n" +
                "                                        t.reasonpur,\n" +
                "                                        t.maintechtors,\n" +
                "                                        t.packform,\n" +
                "                                        t.qualitygrade,\n" +
                "                                        t.manufacturer,\n" +
                "                                        t.advisupplier,\n" +
                "                                        t.orderunit,\n" +
                "                                        t.orderqty,\n" +
                "                                        t.unitcost,\n" +
                "                                        t.linecost,\n" +
                "                                        t.purchasemethod,\n" +
                "                                        a.deptcode,\n" +
                "                                        (select fullname from sys_dept where code=a.deptcode) as fullname,\n" +
                "                                        t.prnum,\n" +
                "                                        b.prcnum,\n" +
                "                                        a.applytime,\n" +
                "                                        t.acceptmentsSelectBox,\n" +
                "                                        t.reqdeliverydate,\n" +
                "                                        d.createtime as createtime1,\n" +
                "                                        e.createtime as createtime2,\n" +
                "                                        g.procurexetor,\n" +
                "                                        i.deliverycycle,\n" +
                "                                        i.commerceorder,\n" +
                "                                        i.ponum,\n" +
                "                                        i.unitcost as unitcost1,\n" +
                "                                        i.orderqty as orderqty1,\n" +
                "                                        i.amexclutax\n" +
                "                                        FROM\n" +
                "                                        gq_prline t\n" +
                "                                        LEFT JOIN gq_pr a ON t.prnum = a.prnum and a.status='已批准'\n" +
                "                                        LEFT JOIN gq_prchange b on a.prnum =b.prnum and b.status='已批准'\n" +
                "                                        left join gq_prtdetailline c on t.id= c.prlid and c.receiveqty>0\n" +
                "                                        left join gq_matusetrans d on c.batchnum = d.lotnum and d.type='接收'\n" +
                "                                        left join gq_matusetrans e on e.lotnum=c.batchnum and e.type in('领用出库','报废出库','销售出库','返厂维修')\n" +
                "                                        left join gq_ppline f on f.prlid=t.id\n" +
                "                                        left join gq_purchaseplan g on g.id=f.ppid and g.status='已批准' \n" +
                "                                        left join gq_poline h on h.prlid =t.id\n" +
                "                                        left join gq_posline i on i.id = h.poslid \n" +
                "                                        where  t.subjectno IS not NULL and t.prnum in (select prnum from gq_pr where prnum=t.prnum)  and g.ppnum IS NOT NULL ";
        if(StringUtils.isNotBlank(ids)){
            sql = sql + " and t.id in (" + ids + ")";
        }
        bySql = handlerService.findBySql(sql);
        return bySql;
    }
}
